import controlMgr from './salesCommon'

import tool from "@/common/tool.js"

let status = null

function initStatus() {
  if (!status) {
    status = {
      sum: {
        key: 'sum',
        value: '总套数',
        extend: ''
      }
    }
    JSON.parse(tool.getStorage('dictionaryData')).salesStatus.forEach(item => {
      status[item.key] = item
    })
  }
}

function salesStatus(keys, nums) {
  initStatus()
  let arr = []
  keys.forEach(key => {
    let item = {
      key,
      title: status[key].value,
      color: status[key].extend
    }
    if (nums) {
      item.value = nums[key]
    }
    arr.push(item)
  })
  return arr
}

function buildingInfos(productId) {
  return controlMgr.roomStatusTotal({
    productId
  }).then((res) => {
    let buildings = []
    if (res && res.buildingStatusTotals) {
      buildings = res.buildingStatusTotals.map(item => {
        let count = {}
        item.statusTotals.forEach(res => {
          count[res.saleStatus] = res.roomCount
        })
        item.count = salesStatus(['3', '4', '2', '5', '6', '1'], count)
        item.isSellable = !item.isSellControl && item.bindBatch
        return item
      })
    }
    let count = {
      sum: res.sumRoom
    }
    if (res && res.statusTotals) {
      res.statusTotals.forEach(item => {
        count[item.saleStatus] = item.roomCount
      })
    }
    let counts = salesStatus(['sum', '5', '6', '3', '4', '2', '1'], count)

    return {
      buildings,
      counts
    }
  })
}

function buildingSellControl(buildingId, isSellControl) {
  return controlMgr.editBuild({
    buildingId,
    isSellControl
  })
}

function roomInfos(buildingId) {
  initStatus()
  return controlMgr.getAllRoom({
    buildingId
  }).then((res) => {
    if (res) {
      res.unitDTOS.forEach((item) => {
        let colsData = []
        let max = res.isImported ? item.maxRoomCount : item.roomSetCount
        for (let index = 0; index < max; index++) {
          colsData.push({
            isSelected: false,
          })
        }
        item.isSelected = false
        item.colsData = colsData
        if (item.floorDTOS) {
          item.floorDTOS.forEach((floorItem) => {
            floorItem.isSelected = false
            if (floorItem.roomDTOS) {
              floorItem.roomDTOS.forEach((roomItem) => {
                roomItem.isSelected = false
                roomItem.statusColor = status[roomItem.sellStatus] ? status[
                  roomItem.sellStatus].extend : ''
                roomItem.unControl = ['5', '6', '2'].indexOf(roomItem
                  .sellStatus) < 0
              })
            }
          })
        }
      })
      return res
    }
  })
}

function buildingCount(buildingId) {
  return controlMgr.roomStatusTotal({
    buildingId
  }).then((res) => {
    if (res && res.buildingStatusTotals && res.buildingStatusTotals[0]) {
      let item = res.buildingStatusTotals[0]
      let count = {
        sum: item.sumRoom
      }
      item.statusTotals.forEach(res => {
        count[res.saleStatus] = res.roomCount
      })
      return salesStatus(['sum', '5', '6', '3', '4', '2', '1'], count)
    }
    return []
  })
}

function roomControlOption() {
  return salesStatus(['5', '6', '2'])
}

function roomControl(rooms, status) {
  let roomEditVOS = []
  rooms.forEach(item => {
    if (!item.unControl) {
      item.sellStatus = status
      delete item.unControl
      delete item.statusColor
      roomEditVOS.push(item)
    }
  })
  return controlMgr.editRoom({ roomEditVOS })
}

export default {
  buildingInfos,
  buildingSellControl,
  roomInfos,
  buildingCount,
  roomControlOption,
  roomControl
}